package com.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mysql.cj.xdevapi.Client;
import com.shop.common.NoUtil;
import com.shop.common.RedisUtil;
import com.shop.common.result.Result;
import com.shop.common.result.util.ResultModel;
import com.shop.entity.*;
import com.shop.mapper.*;
import com.shop.service.ShopService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shop.shopEnum.NoEnum;
import com.shop.shopEnum.RoleEnum;
import com.shop.shopEnum.YesOrNoEnum;
import com.shop.vo.AddShopVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;

/**
 * <p>
 * 店主表 服务实现类
 * </p>
 *
 * @author 靳 旺
 * @since 2021-03-14
 */
@Service
public class ShopServiceImpl extends ServiceImpl<ShopDoMapper, ShopDo> implements ShopService {

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private ClientDoMapper clientDoMapper;

    @Autowired
    private ClientRoleRelationDoMapper clientRoleRelationDoMapper;

    @Autowired
    private WarehouseInfoDoMapper warehouseInfoDoMapper;

    @Autowired
    private ShopWarehoseRelationDoMapper shopWarehouseRelateDoMapper;

    @Override
    public ResultModel addShop(AddShopVo addShopVo) throws Exception {
        if (addShopVo.getPhone().length() != 11)
            throw new Exception("请输入正确手机号！");
        /* 1、验证短信码 */
//        this.checkingVerificationCode(addShopVo.getVerificationCode(), addShopVo.getPhone());
        /* 2、查询用户信息 */
        ClientDo clientDo = this.selectClient(addShopVo.getPhone());
        /* 3、添加商铺 */
        ShopDo shopDo = this.addShop(clientDo);
        /* 4、添加默认仓库 */
        this.addDefaultWarehouse(shopDo);
        /* 5、添加用户角色卖家信息 */
        this.addClientRoleRelation(clientDo);

        return Result.successResultModel();
    }

    private ClientDo selectClient(String phone) throws Exception {
        QueryWrapper<ClientDo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phone", phone);
        List<ClientDo> clientDoList = clientDoMapper.selectList(queryWrapper);
        if (clientDoList.size() == 0) throw new Exception("没有找到用户！");
        return clientDoList.get(0);
    }

    private Boolean checkingVerificationCode(String verificationCode, String clientPhone) throws Exception {
        String phone = null;
        try {
            phone = redisUtil.get(verificationCode).toString();
        } catch (Exception e) {
            throw new Exception("验证码不正确！");
        }
        Assert.notNull(phone, "验证码不正确！");
        if (!phone.equals(clientPhone)) return false;
        return true;
    }

    /**
     * 添加店铺
     */
    private ShopDo addShop(ClientDo clientDo) throws Exception {

        QueryWrapper<ShopDo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("fk_client_id", clientDo.getPkClientId());
        List<ShopDo> shopDoList = baseMapper.selectList(queryWrapper);
        if (shopDoList.size() > 0) throw new Exception("您已开通过店铺，不可重复开店铺！");

        ShopDo shopDo = new ShopDo();
        shopDo.setFkClientId(clientDo.getPkClientId());
        shopDo.setShopName(String.format("%s的店铺", clientDo.getClientName()));
        shopDo.setShopGrade("1星");
        if (baseMapper.insert(shopDo) <= 0) throw new Exception("添加店铺失败！");

        QueryWrapper<ShopDo> shopDoQueryWrapper = new QueryWrapper<>();
        shopDoQueryWrapper.eq("fk_client_id",shopDo.getFkClientId());
        return  baseMapper.selectOne(shopDoQueryWrapper);
    }

    /**
     * 添加卖家权限
     */
    private void addClientRoleRelation(ClientDo clientDo) throws Exception {
        ClientRoleRelationDo clientRoleRelationDo = new ClientRoleRelationDo();
        clientRoleRelationDo.setFkClientId(clientDo.getPkClientId());
        clientRoleRelationDo.setFkRoleId(RoleEnum.SELLER.id);
        clientRoleRelationDo.setCreateId(clientDo.getPkClientId());
        if (clientRoleRelationDoMapper.insert(clientRoleRelationDo) <= 0) throw new Exception("添加卖家权限失败！");
    }


    /**
     * 添加默认仓库
     * @param shop
     */
    private void addDefaultWarehouse(ShopDo shop){
        ClientDo clientDo = clientDoMapper.selectById(shop.getFkClientId());

        WarehouseInfoDo warehouseInfoDo = new WarehouseInfoDo();
        warehouseInfoDo.setWarehouseNo(NoUtil.generator(NoEnum.WAREHOUSE));
        warehouseInfoDo.setWarehoustName(shop.getShopName()+"默认仓库");
        warehouseInfoDo.setWarehousePhone(clientDo.getPhone());
        warehouseInfoDo.setIsDefaultWarehouse(YesOrNoEnum.YES.code);
        warehouseInfoDo.setWarehouseStatus(YesOrNoEnum.YES.code);
        warehouseInfoDo.setCreateId(shop.getFkClientId());
        warehouseInfoDoMapper.insert(warehouseInfoDo);

        ShopWarehoseRelationDo shopWarehoseRelationDo = new ShopWarehoseRelationDo();
        shopWarehoseRelationDo.setFkShopId(shop.getPkShopId());
        shopWarehoseRelationDo.setFkWarehoseInfoId(warehouseInfoDo.getPkWarehouseInfoId());
        shopWarehouseRelateDoMapper.insert(shopWarehoseRelationDo);
    }

}
